package com.config;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.mapper.helping.IssueMapper;
import com.mapper.helping.LabelMapper;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.util.HotUtil;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.Arrays;
import java.util.concurrent.*;

import static java.util.concurrent.TimeUnit.HOURS;

@Configuration
@RequiredArgsConstructor
@Slf4j
class WholeConfig {
    private final HotUtil hotUtil;
    private final IssueMapper issueMapper;
    private final LabelMapper labelMapper;
    @Bean
    public IClientConfig iClientConfig() {
        return new DefaultClientConfigImpl();
    }

    @Bean
    public MybatisPlusProperties  mybatisPlusProperties(){return new MybatisPlusProperties();};

    @Bean
    public NacosDiscoveryProperties nacosDiscoveryProperties(){return new NacosDiscoveryProperties();}

    @Bean("DhyExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("DhyExecutor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        return executor;
    }

    @Bean("sendMessage")
    public TaskExecutor taskExecutor2() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("sendMessage-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        executor.initialize();
        return executor;
    }

    /**
     * 应用启动时,开启热度减少功能
     */
    @Bean
    public CommandLineRunner HotConfig(){
        return new CommandLineRunner() {
            @Override
            public void run(String... args){
              //是否自动开启热度减少功能
                if(hotUtil.isAutoHotReduce())
              {
                  log.info("热度开始减少....");
                  doReduceHot();
              }
            }
        };
    }

    private void doReduceHot()
    {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        //每隔一小时减少一次热度
        executorService.scheduleAtFixedRate(()->{
           log.info("减少热度中....");
          //减少所有问题和标签的热度
          issueMapper.reduceHot(hotUtil.getISSUE_HOUR_REDUCE_HOT());
          labelMapper.reduceHot(hotUtil.getLABEL_HOUR_REDUCE_HOT());
        },1,1,HOURS);
    }
}
